<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 下午 1:54
 */
namespace app\member\controller;
use app\common\api\AApi;
use app\common\api\member\IMember;


class Api extends AApi implements IMember {
    /**
     * 初始化配置
     * @param array $config
     */
    public function __construct(array $config = []){
        parent::__construct();
    }

    /**
     * 用户对象
     */
    static public $obj;

    /**
     * 单列模式实例化对象
     */
    static function getInstance()
    {
        if (empty(self::$obj)) {
            $data = self::$post_data;
            $type = null;

            if(isset($data['type'])){
                $type = $data['type'];
            }

            switch($type){
                case 'email':
                    #邮箱登录
                    break;

                case 'phone':
                    #手机号登录
                    break;

                default:
                    #账号密码
                    self::$obj = new Member($data);
            }
            return self::$obj;
        } else {
            return self::$obj;
        }
    }

    /**
     * 登录
     * @return mixed
     */
    public function login(){
        $obj = self::getInstance();
        $res = $obj ->login();

        if($res['code'] === 200){
            $temp = json_decode(json_encode($res['data']),true);
            $temp['cid'] = db('member')->where(['id'=>$temp['id']])->value('id');

            session('admin_info',$temp);
        }
        return create_callback($res);
    }

    /**
     * 注册
     * @return mixed
     */
    public function register(){
        $obj = self::getInstance();
        $res = $obj ->register();
        return create_callback($res);
    }

    /**
     * 获取用户详情
     * @return mixed
     */
    public function info(){
        $obj = self::getInstance();
        $res = $obj ->info();
        return create_callback($res);
    }

    /**
     * 更改用户信息
     * @return mixed
     */
    public function update(){
        $obj = self::getInstance();
        $res = $obj ->update();
        return create_callback($res);
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function user_list(){
        $obj = self::getInstance();
        $res = $obj ->user_list();
        return create_callback($res);
    }

    /**
     * 用户删除
     * @return array|string
     */
    public function del()
    {
        $obj = self::getInstance();
        $res = $obj ->del();
        return create_callback($res);
    }

    /**
     * 批量导入用户
     */
    public function user_import()
    {
        $obj = self::getInstance();
        $res = $obj ->user_import();
        return create_callback($res);
    }

    /**
     * 批量导出用户信息
     * @return array|string
     */
    public function user_export()
    {
        $obj = self::getInstance();
        $res = $obj ->user_export();
        return create_callback($res);
    }
}